﻿using System;
using System.Net;
using System.ServiceModel.Web;

namespace suanpan.API.Components.Exceptions
{
    public class RESTException : Exception
    {
        private bool suppressEntityBody = true;
        public HttpStatusCode StatusCode { get; set; }
        public string StatusDescription { get; set; }

        public RESTException(Exception ex)
            : this(ex, HttpStatusCode.InternalServerError)
        {
        }
        public RESTException(Exception ex, HttpStatusCode httpStatusCode)
            : this(ex, httpStatusCode, null)
        {
            
        }
        public RESTException(Exception ex, HttpStatusCode httpStatusCode, string statusDescription)
            : base(ex.Message, ex)
        {
            this.StatusCode = httpStatusCode;
            this.StatusDescription = statusDescription;
        }
        public void SetResponse(bool suppressEntityBody)
        {
            this.suppressEntityBody = suppressEntityBody;
            SetResponse();
        }
        public virtual void SetResponse()
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = this.StatusCode;
            WebOperationContext.Current.OutgoingResponse.StatusDescription = this.StatusDescription;
            WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = suppressEntityBody;
        }
    }
}
